package com.shanpin.shebao.admin.order;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.shanpin.core.constant.PayForConst;
import com.shanpin.shebao.admin.IASql;
import com.shanpin.shebao.admin.db.AppDbRepository;
import com.shanpin.shebao.admin.module.shebao.SBConfigDao;
import com.shanpin.shebao.admin.module.shebao.model.ShebaoConfigData;
import com.shanpin.shebao.admin.order.model.OrderBaseVO;
import com.shanpin.shebao.model.PayOrderDetail;
import com.shanpin.shebao.utility.PageModel;

@Repository
public class PayedDao {

	
	private Logger logger = LoggerFactory.getLogger(PayedDao.class);

	@Autowired
	private AppDbRepository jdbc;
	@Autowired
	private SBConfigDao sbConfigDao;
	@Autowired
	private RepayRefundDao repayRefundDao;
	@Autowired
	private PayedDetailDao payedDetailDao;
	
	/**
	 * 多个身份证批量查询的查询代码。
	 * @param pm
	 * @param idcards
	 * @param for_month
	 * @return
	 */
//	protected PageModel queryBatchData(PageModel pm, Map<String, Object> map, boolean hasMonth) {
//		String sql = PayedDetailDao.QUERY_DETAIL_SQL;
//		if (hasMonth) {
//			sql = sql + " WHERE p.for_month=:for_month AND p.id_card IN (:idcard) order by p.gen_time desc";
//		} else {
//			sql = sql + " WHERE p.id_card IN (:idcard) order by p.gen_time desc";
//		}
//		
//		Map<String, Float> userNeedRepay = repayRefundDao.userRepay();
//		List<PayOrderDetail> recordList = jdbc.queryByNamed(sql, map, PayOrderDetail.detailMapper);
//		for(PayOrderDetail payOrderDetail: recordList) {
//			if(payOrderDetail == null) {
//				continue;
//			}
////			if (hideIdcard) {
////				payOrderDetail.setId_card(payOrderDetail.getId_card()
////						.replaceAll("(?<=\\w{2})\\w(?=\\w{2})", "*"));
////			}
//			ShebaoConfigData configData = sbConfigDao.getShebaoForOrder(payOrderDetail.getCid());
//			if (null == configData) {
//				logger.error("found ShebaoConfigData Object NULL order_no:{}", payOrderDetail.getOrder_no());
//			}
//			payOrderDetail.setParam_name(null == configData ? "----" : configData.getShebao_type());
//			payOrderDetail.setDec_date(null == configData ? 0 : configData.getDec_date());
//			payedDetailDao.setOrderExtend(payOrderDetail, userNeedRepay);
//			payedDetailDao.setCouponUser(payOrderDetail);
//			payedDetailDao.setFundSalary(payOrderDetail);
//		}
//		pm.setRecordList(recordList);
//		pm.setTotalRecord(recordList.size());
//		return pm;
//	}
	
	public PageModel queryBatchData(PageModel pm, Map<String, Object> map, String partSql) {
		String sql = PayedDetailDao.QUERY_DETAIL_SQL + partSql + "  order by p.gen_time desc";
		List<PayOrderDetail> recordList = jdbc.queryByNamed(sql, map, PayOrderDetail.detailMapper);
		for(PayOrderDetail payOrderDetail: recordList) {
			if(payOrderDetail == null) {
				continue;
			}
//			if (hideIdcard) {
//				payOrderDetail.setId_card(payOrderDetail.getId_card()
//						.replaceAll("(?<=\\w{2})\\w(?=\\w{2})", "*"));
//			}
			ShebaoConfigData configData = sbConfigDao.getShebaoForOrder(payOrderDetail.getCid());
			if (null == configData) {
				logger.error("found ShebaoConfigData Object NULL order_no:{}", payOrderDetail.getOrder_no());
			}
			payOrderDetail.setParam_name(null == configData ? "----" : configData.getShebao_type());
			payOrderDetail.setDec_date(null == configData ? 0 : configData.getDec_date());
			payedDetailDao.setOrderExtend(payOrderDetail);
			payedDetailDao.setCouponUser(payOrderDetail);
			payedDetailDao.setFundSalary(payOrderDetail);
		}
		pm.setTotalRecord(recordList.size());
		if (recordList.size() > 0) {
			pm.setPageSize(pm.getTotalRecord());
		}
		pm.setRecordList(recordList);
		return pm;
	}

	
	public List<PayOrderDetail> queryList(String sql ,Map<String, Object> paramMap) {
		List<PayOrderDetail> list = jdbc.queryByNamed(sql, paramMap, PayOrderDetail.buyOrderMapper);
		return list;
	}
	
	public int queryListCount(String sql ,Map<String, Object> paramMap) {
		return jdbc.queryInt(sql, paramMap);
	}
	

	public PayOrderDetail queryBuyUser(String buy_no) {
		return jdbc.queryT(PayOrderDetail.BUYUSER_SQL, PayOrderDetail.buyuserMapper, buy_no);
	}
	
	
	public OrderBaseVO getOrderBase(String order_no) {
		return jdbc.queryT(IASql.ORDER_CID_UNAME_BASE, OrderBaseVO.mapper, order_no);
	}
	public OrderBaseVO getOrderNoStatus(int city_code, int for_month, PayForConst payFor, String id_card) {
		return jdbc.queryT(IASql.ORDER_STATUS, OrderBaseVO.orderNOStatusMapper, city_code,
				for_month, payFor.getNum(), id_card);
	}
}
